<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 示例接口
 */
class Demo extends Api
{


    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test1','voiceTts','getvolice','sendUrl','getV'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    /**
     * 无需登录的接口
     * 
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     * 
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     * 
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }
  
    public function voiceTts($content, $output_path){
    $param = ['engine_type' => 'intp65',
              'auf' => 'audio/L16;rate=16000',
              'aue' => 'raw',
              'voice_name' => 'xiaoyan',
              'speed' => '0'
      ];
        $cur_time = (string)time();
        $x_param = base64_encode(json_encode($param));
        $header_data = [
                        'X-Appid:'.'5b28f09b',
                        'X-CurTime:'.$cur_time,
                        'X-Param:'.$x_param,
                        'X-CheckSum:'.md5('bda435b74e06055c78923f7be0a12280'.$cur_time.$x_param), 
                        'Content-Type:application/x-www-form-urlencoded; charset=utf-8',
                        'X-Real-Ip:127.0.0.1',
        ];
    $body_data = 'text='.urlencode($content);    //Request
    $url = "http://api.xfyun.cn/v1/service/v1/tts"; 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body_data);
    $result = curl_exec($ch); 
    $res_header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $res_header = substr($result, 0, $res_header_size);
    curl_close($ch); 
    if(stripos($res_header, 'Content-Type: audio/mpeg') === FALSE){ //合成错误
        return substr($result, $res_header_size);
    }else{
        file_put_contents($output_path, substr($result, $res_header_size)); 
               return '语音合成成功，请查看文件！';
    }
}
public function   getvolice(){
    echo $this->voiceTts('科大讯飞真给力,hello world ,my name is 张三', 'volice/test_test.wav');
}



public function sendUrl($url,$method,$method_get = 'post',$parames,$headers=array()){
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,60); 
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_HEADER, true);
  curl_setopt($curl, CURLOPT_URL,"http://api.xfyun.cn/v1/service/v1/tts");
  if($method=='https'){
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  }
  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  if($method=='get'){
    if(is_array($parames)){
       $url = $url .(is_numeric(strpos($url,'?'))?'&':'?').http_build_query($parames);    

    }else{
       $url = $url .(is_numeric(strpos($url,'?'))?'&':'?').$parames;    
    }
    //$ulr =  'http://www.tp5.cn/index.php/admin/index/hi';
  }else{
     curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $parames);  
          
  }

  $data = curl_exec($curl);
  return $data; 
}
public function getV(){
  $url = 'http://api.xfyun.cn/v1/service/v1/tts';
$method = 'http';
$method_get = 'post';
$time = (string)time();
$appid = '5b28f09b';
$appkey = 'bda435b74e06055c78923f7be0a12280';
$parames_header = array(
  'auf'=>'audio/L16;rate=16000',
  'aue'=>'raw',
  'voice_name'=>'xiaoyan',
  'speed'=>0,
  'engine_type'=>'intp65',
  'X-Real-Ip:127.0.0.1',
);

$checkSum = MD5($appkey.$time.base64_encode(json_encode($parames_header)));
$parames_header = base64_encode(json_encode($parames_header));
$headers = array(
              'X-CurTime:'.$time,
                'X-Param:'.$parames_header,
                'X-Appid:'.$appid,
                'X-CheckSum:'.$checkSum,
                'X-Real-Ip:127.0.0.1',
                "Content-Type:application/x-www-form-urlencoded;charset='utf-8'",
 );
$parames_body = 'text='.urlencode('我是张三,你是哪位?');
$data = $this->sendUrl($url,$method,$method_get,$parames_body,$headers);

  echo $data;die;
}










}
